﻿using System.Collections.Generic;
using System.Configuration;
using EmbeddedSharp.Core.Logging;

namespace EmbeddedSharp.Core {
    public class DefaultDependencyManager {

        private static DefaultDependencyManager dm = new DefaultDependencyManager();

        private IConstantManager constantManager = null;

        private IFunctionManager functionManager = null;

        private IExpressionTranslator exprTranslator = null;

        private IExpressionParserFactory parserFactory = null;

        private DefaultFunctionLoader dfl = null;

        private ILogger logger = null;

        protected DefaultDependencyManager() {
        }

        public static DefaultDependencyManager Instance {
            get {
                return dm;
            }
        }

        public IExpressionParserFactory ParserFactory {
            get {
                if (parserFactory == null) {
                    parserFactory = new DefaultParserFactory();
                }

                return parserFactory;
            }
            set {
                this.parserFactory = value;
            }
        }

        public IConstantManager ConstantManager {
            get {
                if (this.constantManager == null) {
                    string fileName = Utils.ConstantFile;
                    this.constantManager = new DefaultConstantManager(fileName);
                }

                return this.constantManager;
            }
            set {
                this.constantManager = value;
            }
        }

        public IFunctionManager FunctionManager {
            get {
                if (this.functionManager == null) {
                    this.functionManager = new DefaultFunctionManager();
                }

                return this.functionManager;
            }
            set {
                this.functionManager = value;
            }
        }

        public IExpressionTranslator ExpressionTranslator {
            get {
                if (this.exprTranslator == null) {
                    this.exprTranslator = new DefaultExpressionTranslator();
                }

                return this.exprTranslator;
            }
            set {
                this.exprTranslator = value;
            }
        }

        public string LogFile {
            get {
                return ConfigurationManager.AppSettings["LogFile"];
            }
        }

        public ILogger Logger {
            get {
                if (this.logger == null) {
                    this.logger = new TextLogger(LogFile);
                }

                return this.logger;
            }
        }
    }
}
